//
//  LBLumaFilter.mm
//  OpenGLGame
//
//  Created by Nguyen Cong Huy on 1/23/13.
//  Copyright (c) 2013 UIT. All rights reserved.
//

#include "LBLumaFilter.h"
char lumaFilterFragStr[] = ""

"precision mediump float;"

"varying vec2 vtexCoor;"
"uniform sampler2D texture;"

//contrast function (default 1.0)
"vec4 contrastColor(vec4 color, float contrast){"
"   color.r = (color.r - 0.5)*contrast + 0.5;"
"   color.g = (color.g - 0.5)*contrast + 0.5;"
"   color.b = (color.b - 0.5)*contrast + 0.5;"
"   color = clamp(color, 0.0, 1.0);"
"   return color;"
"}"
//END contrast function

// vignette function
"vec4 vignette(vec4 color, vec4 vignetteColor, vec2 center, highp float begin, highp float end, float intensity, vec2 texCoor){"
"   float d = distance(texCoor, vec2(center.x, center.y));"
"   float percent = smoothstep(begin, end, d) * intensity;"
"   vec4 result = vec4(mix(color.r, vignetteColor.x, percent), mix(color.y, vignetteColor.y, percent), mix(color.z, vignetteColor.z, percent), 1.0);"
"   return result;"
"}"
// END vignette function

//brightness function (default 0.0)
"vec4 brightnessColor(vec4 color, float brightness){"
"   color.r = color.r + brightness;"
"   color.g = color.g + brightness;"
"   color.b = color.b + brightness;"
"   color = clamp(color, 0.0, 1.0);"
"   return color;"
"}"
//END brightness function

// function whiteBalance
"vec4 whiteBalance(vec4 color, float temperature, float tint){"
"   const mediump mat3 RGBtoYIQ = mat3(0.299, 0.587, 0.114, 0.596, -0.274, -0.322, 0.212, -0.523, 0.311);"
"   const mediump mat3 YIQtoRGB = mat3(1.0, 0.956, 0.621, 1.0, -0.272, -0.647, 1.0, -1.105, 1.702);"
"   const mediump vec3 warmFilter = vec3(0.93, 0.54, 0.0);"

"   if(temperature < 5000.0) temperature = 0.0004 * (temperature - 5000.0);"
"   else temperature = 0.00006 * (temperature - 5000.0);"

"	mediump vec3 yiq = RGBtoYIQ * color.rgb;"
"	yiq.b = clamp(yiq.b + tint*0.5226*0.1, -0.5226, 0.5226);"
"	lowp vec3 rgb = YIQtoRGB * yiq;"

"	vec3 processed = vec3("
"                              (rgb.r < 0.5 ? (2.0 * rgb.r * warmFilter.r) : (1.0 - 2.0 * (1.0 - rgb.r) * (1.0 - warmFilter.r))), "
"                              (rgb.g < 0.5 ? (2.0 * rgb.g * warmFilter.g) : (1.0 - 2.0 * (1.0 - rgb.g) * (1.0 - warmFilter.g))), "
"                              (rgb.b < 0.5 ? (2.0 * rgb.b * warmFilter.b) : (1.0 - 2.0 * (1.0 - rgb.b) * (1.0 - warmFilter.b))));"


"    vec4 result = vec4(mix(rgb, processed, temperature), color.a);"
"    return result;"
"}"
// END function whiteBalance


//saturation function (default 1.0)
"vec4 saturationColor(vec4 color, float saturation){"
"  float avg = (color.r + color.g + color.b)/3.0; "
"  color.r = avg + saturation*(color.r - avg); "
"  color.g = avg + saturation*(color.g - avg);"
"  color.b = avg + saturation*(color.b - avg);"
"   color = clamp(color, 0.0, 1.0);"
"  return color;"
"}"
//END saturation function

"void main()"
"{"
"   lowp vec4 textureColor = texture2D(texture, vtexCoor);"
"   vec4 result = textureColor;"

"   result = vignette(result, vec4(0.07, 0.16, 0.03, 1.0), vec2(0.5, 0.5), 0.46, 0.75, 1.0, vtexCoor);"
"   result = contrastColor(result, 1.1);"
"   result = whiteBalance(result, 5700.0, 0.0);"
"   result = brightnessColor(result, 0.1);"
"   result = saturationColor(result, 1.3);"

"   gl_FragColor = result;"
"}";

LBLumaFilter::LBLumaFilter(){
    init(0, lumaFilterFragStr);
}